<?php 
/**
 * 公用的函数方法
 */
 
// 返回json数据方法  状态码 消息  json数据  $data不传时为空
 function show($status,$message,$data=array()){
 	$result = array(
	'status'=>$status,
	'message'=>$message,
	'data'=>$data,
	);
	
	exit(json_encode($result));
 }
 
 //获取md5加密密码
 function getMd5Password ($password)
 {
 	return md5($password . C('MD5_PRE'));
 }
 
 
 function getMenuType($type) {
 	return $type == 1 ? '后台菜单' :'前台导航';
 }
 
 function getMenuStatus($status) {
 	if($status==0) {
 		$str='关闭';
 	}elseif($status==1) {
 		$str='开启';
 	}elseif($status==-1) {
 		$str='删除';
	}
 	return $str;
 }
 
 function checkStatus($status) {
 	if($status==0) {
 		$str='待审核';
 	}elseif($status==1) {
 		$str='已审';
 	}elseif($status==-1) {
 		$str='删除';
	}
 	return $str;
 }
function status ($status) {
	if($status==0) {
 		$str='草稿';
 	}elseif($status==1) {
 		$str='正常';
 	}elseif($status==-1) {
 		$str='已删除';
	}
 	return $str;
}
 function getAdminMenuUrl ($nav){//获取菜单链接
 	$url='/admin.php?c='.$nav['c'].'&a='.$nav['a'];
	if($nav['f']=='index'){
		$url='/admin.php?c='.$nav['c'];
	}
	return $url;
 }

 function getActive ($navc){//获取高亮
 	$c = strtolower(CONTROLLER_NAME);
	if(strtolower($navc)==$c){
		return 'active';
	}
	return '';
 }

 function getLoginUsername (){//获取登录用户名
 	return $_SESSION['adminUser']['realname'] ? $_SESSION['adminUser']['realname']:'未知';
 }
 
function getCatName ($navs,$id){//获取前端栏目名字
	foreach($navs as $nav){
		$navList[$nav['menu_id']] = $nav['name'];
	}	
	return isset($navList[$id]) ? $navList[$id] : '';
}

function getPositionName ($names,$id){//获取推荐位名字
	foreach($names as $name){
		$nameList[$name['id']] = $name['name'];
	}	
	return isset($nameList[$id]) ? $nameList[$id] : '';
}
function getCopyFromById ($id) {
	$copyFrom = C('COPY_FROM');
	return $copyFrom[$id] ? $copyFrom[$id] : '';
}

function isThumb ($thumb) {
	if($thumb){
		return '<span style="color:red">有</span>';
	}
	return '无';
}
function substr_cut($length,$str_cut){
    if (strlen($str_cut) > $length){
        for($i=0; $i < $length; $i++)
        if (ord($str_cut[$i]) > 128)    $i++;
        $str_cut = mb_substr($str_cut,0,$i, 'utf-8')."...";
    }
    return $str_cut;
}

function get_qrcode_img ($content){
	import('Vendor.Phpqrcode.Phpqrcode');
	$QRcode = new \QRcode();
	$timestr = time();
	$code = $content; //内容客户端id加sessionid
	$qr_eclevel = 'H';
	$picsize = 6;
	$QRcode::png($code, 'qrcode.png', $qr_eclevel, $picsize);
	$logo = 'logo.png';
	$QR = 'qrcode.png';
	$data = array(
		'qrcode' => $code,
	);
	$qrCodeId = D('Qrcode')->insert($data);
	if($qrCodeId){
		if ($logo !== FALSE) { 
		    $QR = imagecreatefromstring(file_get_contents($QR)); 
		    $logo = imagecreatefromstring(file_get_contents($logo)); 
		    $QR_width = imagesx($QR);
		    $QR_height = imagesy($QR);
		    $logo_width = imagesx($logo);
		    $logo_height = imagesy($logo);
		    $logo_qr_width = $QR_width / 5; 
		    $scale = $logo_width/$logo_qr_width; 
		    $logo_qr_height = $logo_height/$scale; 
		    $from_width = ($QR_width - $logo_qr_width) / 2; 
		    //重新组合图片并调整大小 
		    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,  
		    $logo_qr_height, $logo_width, $logo_height); 
		} 
		//输出图片 
		imagepng($QR, 'myxzy.png');
		echo '<input id="qrcode" name="qrcode" value="'.$code.'" required="" type="hidden">';
		echo '<div id="cs_qrpic"><img src="http://localhost/myxzy.png" ></div>';
		
	}
	
}


function apiShow ($msg='',$code=300,$data=''){
    $result = array(
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    );
    exit(json_encode($result));
}

function api_error ($msg='error',$data = array()) {
    $result = array(
        'status'=>false,
        'code' => 110,
        'msg' => $msg,
        'data' => $data
    );
    exit(json_encode($result));
}

function api_success ($msg='success',$data = array()) {
    $result = array(
        'status'=>true,
        'code' => 200,
        'msg' => $msg,
        'data' => $data
    );
    exit(json_encode($result));
}

//function getTree($data, $pId)
//{
//$html = '';
//foreach($data as $k => $v)
//{
// if($v['parentid'] == $pId)
// {         //父亲找到儿子
//  $activeclass = getActive($v['c']);
// 	$href = getAdminMenuUrl($v);
// if( $v['data']=='class' ){//父级空菜单
// $href = 'javascript:void(0);';
// }
//	$html.= "<li class='sub-menu ".$activeclass."'>";
//	$html.= "<a href=".$href."><i class='fa fa-bar-chart-o'></i><span>".$v['name']."</span><i class='arrow fa fa-angle-right pull-right'></i></a>";
//	$html.= getTree($data, $v['menu_id']);      
//	$html = $html."</li>";
//
// }
//}
//return $html ? '<ul class="nano-content">'.$html.'</ul>' : $html ;
//}

function getTree($data, $pId)
{
$tree = '';
foreach($data as $k => $v)
{
   if($v['parentid'] == $pId)
   {         //父亲找到儿子
    $v['parentid'] = getTree($data, $v['menu_id']);
    $tree[] = $v;
    //unset($data[$k]);
   }
}
return $tree;
}





